#include "Context.h"
#include "Game.h"
#include "Player.h"
#include "Card.h"

#include <iostream>

Context::Context(Game* game)
{
	_game = game;
}

Context::~Context(void)
{
	_game = NULL;
}

enum MenuContext
{
	Preamble = -1,
	Round1,
	Round2,
	Round3,
	Final
};

enum Actions
{
	Hit = 1,
	Stay
};

enum EventTypes
{
	Win,
	Fat
};

void Context::RoundContext(int round)
{
	if(round > Final)	EventContext(Win);
	switch(round)
	{
	case Preamble:
		std::cout << 
			"As you push the salloon doors open you are immediately greeted\n"
			"by an unremitting odor that only serves to further reinforce the\n"
			"quaint 'Establishment of Ill Repute' aesthetic already prominently advertised\n"
			"by the severely dilapidated facade and gnarled horses tied outside.\n"
			"A sensation gnaws at the back of your skull that you entered this den of\n"
			"depravity for a very specific purpose, though all such memories elude you.\n"
			"Awash with the numerous smells and general cacophony that permeates\n"
			"the tiny backwater watering hole, you can barely make out the 'gentleman'\n"
			"in the far corner beckoning you over. As you approach, fingers nervously\n"
			"fidgeting with your gun holster, you see the gentleman in question is\n"
			"decked in regular dealer attire and is cordially inviting you to fill\n"
			"a recently vacated chair by the Blackjack table. You also notice the\n"
			"acrid smell of gunpowder wafting through the air getting stronger\n"
			"as your proximity to the table lessens.\n"
			"The savage law of the west permits no misdeeds, and your presumed\n"
			"predecessor learned this the hard way.\n"
			"You take a seat and give a curt nod to the grizzled fellow sitting\n"
			"to your immediate left. His stare remains fixed at the dealer's deck,\n"
			"likely prepared to disembowel him if he suspects foul play.\n"
			"'Welcome, honored gambler of Yore!', the dealer greets you.\n"
			"You respond by throwing a grime encrusted dollar bill onto the table.\n"
			"The dealer smirks, looks at you and says,\n"
			"'Alright, let me just get the deck prepared..'\n" 
			"The dealer deftly shuffles the deck, under constant scrutiny\n" 
			"of your gambling cohort's unerring gaze.\n";
		break;
	case Round1:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"The dealer deftly provides everyone with two cards each and turns to you.\n"
				"You can't quite place it, but something feels amiss..\n"
				"'[1] Hit, or\n[2] Stay.'\n\n"
				"You stare at the cards on the table, stricken with indecision..\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"Though your neighbor unerringly glares at the dealer, his opposite side\n"
				"obscured like the dark side of the moon, you can't escape the feeling\n"
				"his other eye is fixed on the cards upon the table. A slight shiver\n"
				"runs down your spine as you watch him mull over his decision\n[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"The dealer barely looks at his hand, instead opting to keep his eyes\n"
				"trained on you. An odd glimmer remains in his eyes as he softly smirks\n"
				"at you, inspiring intimidation..\n"
				"He looks down briefly at his hand..\n\n";
			break;
		}
	case Round2:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"You nervously fidget with your cards under the dealer's piercing gaze.\n"
				"Suffused with an inexplicable dread, you muster all your courage and decide..\n"
				"[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"Your cohort shifts slightly in his seat, startling you somewhat.\n"
				"You glance to your side, though all you make out is his towering constitution.\n"
				"[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"The dealer glances at you, his smirk once again widening for a moment,\n"
				"and says in his silky voice,\n"
				"'Sir, you look a bit pale, are you quite alright?'.\n"
				"You respond only with a slight nod. Pleased, the dealer decides..\n\n";
			break;
		}
	case Round3:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"You wipe your brow, sweat draping your oddly pallid skin.\n"
				"You look up at the dealer for but a moment, and almost recoil\n"
				"in agony as your eyes make contact. His calm demeanor unnerves you\n"
				"almost as much as the sense it belies something.. Strange.\n"
				"The dealer softly motions towards you, prompting you to choose..\n[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"You can hear your 'friend's' breathing is strained, and though he\n"
				"remains observant of only the dealer, you can see his massive frame\n"
				"trembling somewhat. With a deep breath, the scarred veteran decides..\n"
				"[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"For but a moment, you swear you can see the shadow cast by the dealer\n"
				"on the wall behind him shift into something.. Ill fated. You look up at\n"
				"him, but see nothing but the usual cool and collected demeanor. He smiles\n"
				"and winks at you, filling you with dread and forcing you to avert your eyes\n"
				"once again..\n\n";
			break;
		}
	case Final:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"You're exhausted by now; A fact which confuses you profoundly.\n"
				"You can barely remember who you are and what you're doing.\n"
				"With incredible strain you pull yourself together to focus..\n"
				"Almost blinded by sweat and dizziness, you squint to make out your hand..\n"
				"[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"The imposing giant beside you is starting to falter now, sweat\n"
				"drenching his unkempt beard and his stature obviously shaken.\n"
				"With one final strenuous breath, he makes his final move..\n"
				"[1] Hit\n[2] Stay\n\n";
			break;
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"Though his exterior radiates nothing but serenity and confidence\n"
				"a sense of impatience has set in his gaze. Not bothering to make snide\n"
				"comments, he takes a gander at his hand..\n\n";
			break;
		}
	}
}

void Context::ActionContext(int action)
{
	switch(action)
	{
	case Hit:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"You begin to move to tap the table signaling the dealer for another card,\n"
				"when the realisation one already lies in front of you gives you pause.\n"
				"The dealer, noticing your nonplussed expression, simply smirks at you\n"
				"before turning to your 'friend'.\n"
				"With slight trepidation you add the dealt card to your hand..\n\n";
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"Your laconic neighbor grunts curtly at the dealer, signaling for another card.\n\n";
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"The dealer deftly flips a card over and places it beside his current hand.\n\n";
		}
		_game->DealCard(_game->currentPlayer, 1);
		_game->PrintHand(_game->currentPlayer);
		if(_game->HandValue(_game->currentPlayer->Hand) > 21)	EventContext(Fat);
		break;
	case Stay:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"Though you're not sure why, you almost have to fight your compulsion\n"
				"to request another card. You raise a trembling hand to signal you're good.\n\n";
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"Your opponent lifts his gargantuan paw aloft and mutters something\n"
				"incomprehensible, a gesture that apparently implies he's staying.\n\n";
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"The dealer's smirk widens momentarily, something that somehow bothers you,\n"
				"and with a silky voice announces, 'Stay'.\n\n";
		}
		std::cout << _game->currentPlayer->Name << " stays\n\n";
		_game->currentPlayer->IsStaying = true;
		break;
	}
}

void Context::EventContext(int context)
{
	switch(context)
	{
	case Win:
		if(_game->currentLeader == NULL || _game->currentLeader->IsFat == true)
		{
			std::cout << 
				"Nobody wins!\n\n";
		}
		else
		{
			if(_game->currentLeader->Name == "Player1")
			{
				std::cout << 
					"In almost an instant your vision clears!\n"
					"Whatever demonic influence fettered you has passed, and memories\n"
					"of your mission come flooding back in a torrent of purpose.\n"
					"With renewed vigor, you rise to your feet and draw your pistol!\n"
					"The 'dealer' snarls at you as his visage morphs before your very eyes.\n"
					"Standing at twice your size, skin ablaze in hellfire and breath\n"
					"heavy with the stench of sulphur and brimstone, the daemon cries\n"
					"out a million curses in its foul tongue.\n"
					"Undeterred, you aim your pistol and fire directly into\n"
					"its blackened heart, dropping the abomination dead upon\n"
					"the scorched hearth..\n\n"
					"Mission successful.\n\n";
			}
			else if(_game->currentLeader->Name == "Player2")
			{
				std::cout << 
					"The grizzled gambler's composure finally breaks at the realization\n"
					"he's won. With a profound and guttural growl he leaps at the dealer,\n"
					"one hand seizing for his target's throat and the other producing\n"
					"a long and vicious blade. They both tumble behind the table from\n"
					"the force of the impact, the sound of furniture splintering and\n"
					"bedlam from the fighting drowning out any other noise within the\n"
					"salloon. Deciding it's likely unwise to wait for another round,\n"
					"you bolt for the door. Before you reach it, however, you find\n"
					"yourself standing in an empty field. What happened? Where am I?\n"
					"A sudden torrent of memories flood you, and you remember your\n"
					"purpose. The daemonic influence on this land has been lifted.\n\n"
					"Mission successful.\n\n";
			}
			else if(_game->currentLeader->Name == "Dealer")
			{
				std::cout << 
					"The dealer's eyes light up, and you can feel a slight tinge upon\n"
					"your skin. A sinister laughter echoes through your mind, and the\n"
					"slight tinge swiftly develops into a burning sensation.\n"
					"The dealer's silhouette twists and contorts before your eyes,\n"
					"his laughter intensifying as his stature grows. Looking down,\n"
					"you realise the immolating sensation is in fact not imaginary.\n"
					"You only catch a tiny glimpse of the final form of the daemon\n"
					"before your body is instantly turned to ash;\n\n"
					"Your soul is forever condemned to hellfire.\n\n";
			}
		}
		break;
	case Fat:
		if(_game->currentPlayer->Name == "Player1")
		{
			std::cout << 
				"A split second before reality even hits you, the dealer says with a wry grin\n"
				"'Oh Lord, my sincerest condolences, for it seems Lady Luck has forsaken you!'\n"
				"You stare at your cards, your mind unable to shake the feeling you have somehow\n"
				"been played for a fool.\n\n";
		}
		else if(_game->currentPlayer->Name == "Player2")
		{
			std::cout << 
				"Your grizzled companion's face contorts into a vicious snarl,\n"
				"and you swear you can feel the fetid air in your otherwise balmy corner\n"
				"of the salloon cools for a moment.\n"
				"The dealer simply smirks and says,\n"
				"'Another inauspicious round, my friend', his voice ripe with insincerity.\n\n";
		}
		else if(_game->currentPlayer->Name == "Dealer")
		{
			std::cout << 
				"Though the dealer's visage remains as static and confident as ever,\n"
				"an odd sensation of hatred permeates your mind, and you swear you can sense\n"
				"a miasmic effervescence not unlike brimstone upon the air.\n"
				"Deciding it would be best not to dwell upon it, you refocus on the game.\n\n";
		}
		std::cout << _game->currentPlayer->Name << " is fat.\n\n";
		_game->currentPlayer->IsFat = true;
		break;
	}
}